<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- el-row/ el-col 属于布局容器 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click="hdit.native('', false)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <!--  default-expand-all 默认展开所有子节点 -->
      <el-tree :data="list" default-expand-all>
        <template v-slot="{data}">
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id, false)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id, true)">编辑部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <!-- 方案1: v-if,最推荐的方案,没有之一 -->
      <!-- v-if 会在DOM上添加或删除,如果用在组件上,就会创建和销毁组件 -->
      <!-- v-if="showDialog"将组件的生命周期和 showDialog 的显示隐藏绑定 -->
      <AddOrEdit v-if="showDialog" :id="curId" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="hClose" />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartment, delDepartmentById } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddOrEdit from './deptDialog.vue'
export default {
  components: {
    AddOrEdit
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 1.封装函数
    async loadDepartments() {
      // 2.try-carth  捕获异常
      try {
        const res = await getDepartment()
        // console.log(res.data.depts)
        // 任务: 复习数组的常用API
        // shift: 删除数组的第一个元素
        // pop : 删除数组的最后一个元素
        // push: 从末尾添加一个元素
        // unShift: 从开头添加一个元素
        // 四个方法都会直接修改原数组

        // 删除数组中第一个元素
        res.data.depts.shift()
        this.list = tranListToTreeData(res.data.depts)
        this.originList = res.data.depts.map(item => ({ id: item.id, pid: item.pid, name: item.name, code: item.code }))
        // 上面代码高级写法
        // this.originList = res.data.depts.map({id,pid,code,name}=>{return {id, name,code, pid}})
      } catch (e) {
        console.log('获取组织架构失败,原因', e)
      }
    },
    hAdd(id) {
      // console.log(id)
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    hDel(id) {
      this.$confirm('是否删除该项目?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartmentById(id)
        this.$message.success(res.message)
        this.loadDepartments()
      }).catch(e => e)
    },
    hClose() {
      this.showDialog = false
    },
    hSuccess() {
      // 收到子组件通知的成功, 报喜
      // 关闭 dialog
      this.showDialog = false
      // 获取最新数据
      this.loadDepartments()
    }
  }
}
</script>
